package io.github.yedaxia.apidocs.doc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;

import io.github.yedaxia.apidocs.DocContext;
import io.github.yedaxia.apidocs.LogUtils;
import io.github.yedaxia.apidocs.Utils;
import io.github.yedaxia.apidocs.parser.AbsControllerParser;
import io.github.yedaxia.apidocs.parser.ControllerNode;

/**
 * 
 * @author wangya
 * @date 2018年12月27日
 */
public abstract class AbsWordDocGenerator{

    private AbsControllerParser controllerParser;
    private WordControllerDocBuilder controllerDocBuilder;
    private List<ControllerNode> controllerNodeList = new ArrayList<>();
    
    private Document document;
	private BaseFont bfChinese;
	
	public AbsWordDocGenerator(AbsControllerParser controllerParser, WordControllerDocBuilder controllerDocBuilder) throws DocumentException, IOException {
        this.controllerParser = controllerParser;
        this.controllerDocBuilder = controllerDocBuilder;
        
        this.document = new Document(PageSize.A4);
		this.bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		this.controllerDocBuilder.setDocument(document);
		this.controllerDocBuilder.setBfChinese(bfChinese);
    }

    /**
     * generate api Docs
     * @throws IOException 
     * @throws DocumentException 
     */
    public void generateDocs() throws DocumentException, IOException{
        LogUtils.info("generate api docs start...");
        generateControllersDocs();
        closeDocument();
        LogUtils.info("generate api docs done !!!");
    }
    
    /**
	 * @param filePath 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
	 * @throws DocumentException
	 * @throws IOException
	 */
	private void openDocument(String filePath) throws DocumentException, IOException {
		// 建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中
		RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));
		this.document.open();
	}

    private void generateControllersDocs() throws DocumentException, IOException{
        File[] controllerFiles = DocContext.getControllerFiles();
        String filepath = DocContext.getDocPath();
        File docPath = new File(filepath);
    	if(!docPath.exists()){
    		docPath.mkdirs();
    	}
    	filepath += "/接口文档.doc";
        // 打开一个文档
        openDocument(filepath);
        controllerDocBuilder.addTitle("接口文档", controllerDocBuilder.getTitle(), Element.ALIGN_CENTER);
        Integer count = 1;
        for (File controllerFile : controllerFiles) {
            try {
                LogUtils.info("start to parse controller file : %s", controllerFile.getName());
                ControllerNode controllerNode = controllerParser.parse(controllerFile);
                if(controllerNode.getRequestNodes().isEmpty()){
                    continue;
                }
                controllerNodeList.add(controllerNode);
                if (Utils.isNotEmpty(controllerNode.getDescription())) {
                	controllerDocBuilder.addTitle(Utils.getUpperNumber(count) + "、" + controllerNode.getDescription(), controllerDocBuilder.getFontSecondTitle(), Element.ALIGN_LEFT);
                }
                // 生成参数列表及返回信息
                controllerDocBuilder.buildDoc(controllerNode);
                count ++;
            } catch (Exception e) {
                LogUtils.error("generate docs for controller file : "+controllerFile.getName()+" fail", e);
            }
        }

    }

    public List<ControllerNode> getControllerNodeList(){
        return controllerNodeList;
    }
    
	/**
	 * 关闭
	 * @throws DocumentException
	 * @throws IOException
	 */
	private void closeDocument() throws DocumentException, IOException {
		this.document.close();
	}
}
